package com.example.network.controller;

import com.example.network.common.Result;
import com.example.network.controller.dto.ShareDTO;
import com.example.network.controller.request.FileRequest;
import com.example.network.service.FileService;
import com.example.network.service.TimeService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.List;

/**
 * @author LR
 */
@Slf4j
@RestController
@RequestMapping("/file")
@Tag(name = "file接口 -黎")
public class FileController{
    @Resource
    private FileService fileService;

    @Resource
    private TimeService timeService;
    /**
     * 搜索文件
     * @param filename
     * @return
     */
    @GetMapping("/searchFile")
    @Operation(summary = "搜索文件")
    public Result searchFilesByFileName(@Parameter(description = "文件名")
                                        @RequestParam String filename,
                                        @Parameter(description = "用户userID")
                                        @RequestParam String userID){
        log.info("用户{}搜索文件:{}",userID,filename);
        timeService.insertTime(userID, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"搜索文件");
        List<FileRequest> requests = fileService.selectByFileName(filename,userID);
        return Result.success(requests);
    }

    /**
     * 上传文件
     *
     * @param multipartFile
     * @return
     */

    @PostMapping("/upload")
    @Operation(summary = "上传文件")
    public Result uploadFile(@RequestParam("chunk") MultipartFile chunk,
                             @RequestParam("fileName") String fileName,
                             @Parameter(description = "分块文件编号")
                             @RequestParam("chunkNumber") int chunkNumber,
                             @Parameter(description = "总分块数")
                             @RequestParam("totalChunks") int totalChunks,
                             @RequestParam("userID") String userID,
                             @RequestParam("chunkIndex") String chunkIndex) {
        String uploadFilestr = fileService.uploadFile(chunk,fileName,chunkNumber,totalChunks,userID,chunkIndex);
        log.info("用户{}上传了文件块:{}",userID,fileName+"("+chunkNumber+"/"+totalChunks+")"+" "+uploadFilestr);
        return Result.success(uploadFilestr);
    }

    /**
     * 删除文件
     * @param filename
     * @return
     */
    @DeleteMapping("/del/{filename}")
    @Operation(summary = "删除文件")
    public Result deleteByFileName(@Parameter(description = "文件名")
                                   @PathVariable String filename ,
                                   @Parameter(description = "用户userID")
                                   @RequestParam String userID){
        log.info("用户{}删除文件:{}",userID,filename);
        timeService.insertTime(userID, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"删除文件");
        fileService.deleteByFileName(filename,userID);
        return Result.success("删除成功");
    }

    /**
     * 下载文件
     *
     * @param filename
     * @param userID
     * @return
     */
    @GetMapping("/download/{filename}")
    @Operation(summary = "下载文件")
    public ResponseEntity<FileSystemResource> downloadFile(@Parameter(description = "文件名")
                                                    @PathVariable String filename,
                                                           @Parameter(description = "用户userID")
                                                    @RequestParam String userID) throws IOException {
        log.info("用户{}下载了文件:{}",userID,filename);
        timeService.insertTime(userID, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"下载文件");
        return fileService.downloadFile(filename,userID);
    }

    /**
     * 查看所有文件
     * @return
     */
    @GetMapping("/selectByAll")
    @Operation(summary = "查看文件")
    public Result selectByAll(@Parameter(description = "用户userID")@RequestParam String userID){
        log.info("用户{}查看文件",userID);
        List<FileRequest> requests = fileService.selectByAll(userID);
        return Result.success(requests);
    }

    /**
     * 创建分享链接
     * @param filePath
     * @return
     */
    @GetMapping("/shareFile")
    @Operation(summary = "创建分享链接")
    public Result shareFile(@Parameter(description = "文件名")
                            @RequestParam String filename,
                            @Parameter(description = "用户userID")
                            @RequestParam String userID){
        log.info("用户{}创建分享链接",userID);
        return Result.success(fileService.shareFile(filename,userID));
    }

    /**
     * 按分享链接搜索文件
     * @param shareID
     * @return
     */
    @GetMapping("/findShareFile/{shareID}")
    @Operation(summary = "搜索分享链接文件")
    public Result findShareFile(@Parameter(description = "分享链接",example = "86032261-5943-4af1-875c-ffc6c6c00181")
                                @PathVariable String shareID,
                                @Parameter(description = "提取码")
                                @RequestParam String filePass,
                                @Parameter(description = "用户的userID",example = "xxx@example.com")
                                @RequestParam String email){
        log.info("用户{}搜索分享链接文件",email);
        ShareDTO shareDTO = fileService.findShareID(shareID,filePass);
        timeService.insertTime(email, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"查看了"+shareDTO.getUserID()+"分享的文件");
        return Result.success(shareDTO);
    }

    /**
     * 下载通过分享链接搜索出来的文件
     *
     * @param filename
     * @param filePass
     * @param downloadUserID
     * @param uploadUserID
     * @return
     */
    @GetMapping("/downloadFileByShareID")
    @Operation(summary = "通过分享链接下载文件")
    public ResponseEntity<FileSystemResource> downloadFileByShareID(@Parameter(description = "下载文件的地址",example = "xxx/xxx/filename")
                                                                    @RequestParam String filePath,
                                                                    @Parameter(description = "下载者的userID",example = "xxx@example.com")
                                                                    @RequestParam String downloadUserID,
                                                                    @Parameter(description = "上传者的userID",example = "xxx@example.com")
                                                                    @RequestParam String uploadUserID){
        log.info("{}下载了{}的文件",downloadUserID,uploadUserID);
        timeService.insertTime(downloadUserID, LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd-HH-mm")),"下载了"+uploadUserID+"的文件");
        return fileService.downloadFileByShareID(filePath);
    }

}
